package cc.mrbird.febs.customer.controller;


import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.customer.entity.Activity;
import cc.mrbird.febs.customer.entity.Customer;
import cc.mrbird.febs.customer.entity.Order;
import cc.mrbird.febs.customer.service.ICustomerService;
import cc.mrbird.febs.customer.service.IOrderService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author Macw
 * @since 2020-11-04
 */
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {

    @Resource
    private IOrderService orderService;

    @Resource
    private ICustomerService customerService;

    @RequestMapping("getOrderList")
    @RequiresPermissions("order:view")
    @ControllerEndpoint(operation = "查询订单", exceptionMessage = "查询订单失败")
    public FebsResponse getOrderList(Order order, QueryRequest request){
//        IPage<Order> page1 = orderService.selectOrderByCondition(order,request);
        Page<Order> page = new Page<>(request.getPageNum(), request.getPageSize());
        LambdaQueryWrapper<Order> orderLambdaQueryWrapper = new LambdaQueryWrapper<Order>()
                .like(StringUtils.isNotBlank(order.getNickName()), Order::getNickName, order.getNickName())
                .like(StringUtils.isNotBlank(order.getTrueName()), Order::getTrueName, order.getTrueName())
                .eq(order.getIsSuccess() != null, Order::getIsSuccess, order.getIsSuccess())
                .like(StringUtils.isNotBlank(order.getOutTradeNo()), Order::getOutTradeNo, order.getOutTradeNo())
                .orderByDesc(Order::getTimeStart);
        /*
         QueryWrapper<Order> orderLambdaQueryWrapper = new QueryWrapper<Order>()
                .like(StringUtils.isNotBlank(order.getNickName()), "nick_name", order.getNickName())
                .like(StringUtils.isNotBlank(order.getTrueName()), "true_name", order.getTrueName())
                .eq(order.getIsSuccess() != null, "is_success", order.getIsSuccess())
                .like(StringUtils.isNotBlank(order.getOutTradeNo()), "out_trade_no", order.getOutTradeNo())
                .orderByDesc("time_start");
         */
        IPage<Order> page2 = orderService.page(page,orderLambdaQueryWrapper );
        List<Order> records = page2.getRecords();
        records.forEach(s->{
            Customer one = customerService.getOne(new QueryWrapper<Customer>()
                    .lambda()
                    .eq(Customer::getOpenid, s.getOpenId()));
            s.setNickName(one.getNickname());
            s.setTrueName(one.getTrueName());
        });
        return new FebsResponse().success().data(getDataTable(page2));
    }


    /**
     * 删除和批量删除
     * @param orderIds
     * @return
     */
    @GetMapping("delete/{orderIds}")
    @RequiresPermissions("order:delete")
    @ControllerEndpoint(operation = "删除订单", exceptionMessage = "删除订单失败")
    public FebsResponse deleteActivity(@NotBlank(message = "{required}") @PathVariable String orderIds) {
        String[] ids = orderIds.split(StringPool.COMMA);
        //获取所有活动id列表
        List<String> list = Arrays.asList(ids);
        //删除活动
        orderService.removeByIds(list);
        return new FebsResponse().success();
    }

}
